<template>
  <div align="center" style="margin-top:13px">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="岗位名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入岗位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作城市" prop="workCity">
        <el-input
          v-model="queryParams.workCity"
          placeholder="请输入工作城市"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      </el-form-item>
      <el-form-item label="截止日期" prop="validDate">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.validDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择截止日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最低薪资" prop="salaryDown">
        <el-input
          v-model="queryParams.salaryDown"
          placeholder="请输入最低薪资"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" @click="handleQuery" plain type="primary">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" plain type="info">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import MyContainer from '@/components/Me/MyContainer'
import { listPosition, getPosition, delPosition, addPosition, updatePosition, exportPosition } from "@/api/recruit/position";
import { mapGetters } from 'vuex'
import { listInfo } from '@/api/recruit/company/info'

export default {
  name: 'Search',
  components: { MyContainer },
  data() {
    return {
      select: '',
      name: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 招聘岗位表格数据
      positionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      // 审核状态字典
      auditStateOptions: [],
      // 是否热门字典
      hotOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        requirement: null,
        workCity: null,
        phonenumber: null,
        releaseDate: null,
        validDate: null,
        quantity: null,
        status: null,
        auditState: null,
        salaryUp: null,
        salaryDown: null,
        companyId: null,
        hot: null,
        categoryId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        requirement: [
          { required: true, message: "要求不能为空", trigger: "blur" }
        ],
        workCity: [
          { required: true, message: "工作城市不能为空", trigger: "blur" }
        ],
        phonenumber: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        releaseDate: [
          { required: true, message: "发布时间不能为空", trigger: "blur" }
        ],
        validDate: [
          { required: true, message: "截止日期不能为空", trigger: "blur" }
        ],
        quantity: [
          { required: true, message: "招聘人数不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        auditState: [
          { required: true, message: "审核状态不能为空", trigger: "blur" }
        ],
        salaryUp: [
          { required: true, message: "最低薪资不能为空", trigger: "blur" }
        ],
        salaryDown: [
          { required: true, message: "最高薪资不能为空", trigger: "blur" }
        ],
        companyId: [
          { required: true, message: "公司ID不能为空", trigger: "blur" }
        ],
        hot: [
          { required: true, message: "是否热门不能为空", trigger: "change" }
        ],
        categoryId: [
          { required: true, message: "分类ID不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
      this.getDicts("recruit_audit_status").then(response => {
        this.auditStateOptions = response.data;
      });
    },
  methods:{
    /** 搜索按钮操作 */
    handleQuery() {
      this.$router.push({path:'/recruit/f/result',query:{queryParams:JSON.stringify(this.queryParams)}})
    },
   getList() {
        if (this.isCompany) {//公司查询自己岗位
          this.queryParams.userId = this.userId
        }
        this.loading = true;
        listPosition(this.queryParams).then(response => {
          this.positionList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 审核状态字典翻译
    auditStateFormat(row, column) {
      return this.selectDictLabel(this.auditStateOptions, row.auditState);
    },
    // 是否热门字典翻译
    hotFormat(row, column) {
      return this.selectDictLabel(this.hotOptions, row.hot);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        requirement: null,
        workCity: null,
        phonenumber: null,
        releaseDate: null,
        validDate: null,
        quantity: null,
        status: null,
        auditState: null,
        salaryUp: null,
        salaryDown: null,
        companyId: null,
        hot: null,
        categoryId: null
      };
      this.resetForm("form");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加招聘岗位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPosition(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改招聘岗位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePosition(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            //根据Userid查询他的企业信息，将公司id设置上
            listInfo({userId:this.userId}).then(res=>{
              this.form.companyId=res.rows[0].id
              addPosition(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            })
          }
        }
      });
    }
  }
}
</script>

<style scoped lang="scss">
.wrapper {
  background-color: #fff;
}
.el-select {
  width: 100px;
}
.el-button {
  font-weight: bold;
}
</style>
